package com.hengtongxing.hejiayun.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hengtongxing.hejiayun.R;
import com.hengtongxing.hejiayun.bean.ShopDetailsBean;
import com.hengtongxing.hejiayun.homepage.BigImageActivity;
import com.hengtongxing.hejiayun.utils.DateTimeUtil;
import com.hengtongxing.hejiayun.utils.Dp2Px;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 董事列表
 */
public class GoodsCommentListAdapter extends RecyclerView.Adapter<GoodsCommentListAdapter.ViewHolder> {

    private List<ShopDetailsBean.DataBean.GoodsCommentBean> listsBeans;
    private Context context;
    private onCallBackListener onCallBackListener;
    private RecyclerView.ItemDecoration itemDecoration;

    public void setOnCallBackListener(GoodsCommentListAdapter.onCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    public GoodsCommentListAdapter(List<ShopDetailsBean.DataBean.GoodsCommentBean> listsBeans) {
        this.listsBeans = listsBeans;
        itemDecoration = getItemDecoration();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_goods_comment_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ShopDetailsBean.DataBean.GoodsCommentBean dataBean = listsBeans.get(position);
        holder.tvName.setText(dataBean.getAgent_name());
        holder.tvContent.setText(dataBean.getComment());
        holder.tvCreateTime.setText(DateTimeUtil.format7(new Date(Long.parseLong(dataBean.getCreate_time()) * 1000)));
        holder.recyclerViewPic.setLayoutManager(new GridLayoutManager(context, 3));
        DynamicPicAdapter adapter = new DynamicPicAdapter(context, dataBean.getImg());
        holder.recyclerViewPic.setAdapter(adapter);
        holder.recyclerViewPic.removeItemDecoration(itemDecoration);
        holder.recyclerViewPic.addItemDecoration(itemDecoration);

        adapter.setListener(position1 -> {
            Intent intent = new Intent(context, BigImageActivity.class);
            intent.putExtra("list", (Serializable) dataBean.getImg());
            intent.putExtra("position", position1);
            context.startActivity(intent);
        });
    }

    private RecyclerView.ItemDecoration getItemDecoration() {
        return new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State
                    state) {
                GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) view.getLayoutParams();
                int spanSize = layoutParams.getSpanSize();
                int spanIndex = layoutParams.getSpanIndex();
                outRect.bottom = (int) Dp2Px.dp2Px(10);
                if (spanSize != 3) {
                    switch (spanIndex) {
                        case 0:
                            outRect.left = (int) Dp2Px.dp2Px(0);
                            outRect.right = (int) Dp2Px.dp2Px(7);
                            break;
                        case 1:
                            outRect.left = (int) Dp2Px.dp2Px(3);
                            outRect.right = (int) Dp2Px.dp2Px(3);
                            break;
                        case 2:
                            outRect.left = (int) Dp2Px.dp2Px(7);
                            outRect.right = (int) Dp2Px.dp2Px(0);
                            break;
                    }
                }
            }
        };

    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_create_time)
        TextView tvCreateTime;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.recyclerView_pic)
        RecyclerView recyclerViewPic;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface onCallBackListener {
        void onCallBack(int type, int position);
    }
}
